#include "tools.h"
#include "sm2.h"

#include <string.h>
/**
 * 测试http://www.pediy.com/kssd/pediy06/pediy6014.htm网页所提及的例子
 */
bool Test_SM2Fp_Sign_Verify() {
#define SIZE 256
	bool r = true;
	SM2Work sm2;
	const size_t size = SIZE;
	uint8_t p[SIZE/8] = { 0x85, 0x42, 0xD6, 0x9E, 0x4c, 0x04, 0x4f, 0x18, 
					  	  0xe8, 0xB9, 0x24, 0x35, 0xBF, 0x6f, 0xf7, 0xde,
						  0x45, 0x72, 0x83, 0x91, 0x5C, 0x45, 0x51, 0x7D,
						  0x72, 0x2E, 0xDB, 0x8B, 0x08, 0xF1, 0xDF, 0xC3 };
	
	uint8_t a[SIZE/8] = { 0x78, 0x79, 0x68, 0xB4, 0xFA, 0x32, 0xC3, 0xFD,
						  0x24, 0x17, 0x84, 0x2E, 0x73, 0xBB, 0xFE, 0xFF,
						  0x2F, 0x3C, 0x84, 0x8B, 0x68, 0x31, 0xD7, 0xE0,
						  0xEC, 0x65, 0x22, 0x8B, 0x39, 0x37, 0xE4, 0x98 };

	uint8_t b[SIZE/8] = { 0x63, 0xE4, 0xC6, 0xD3, 0xB2, 0x3B, 0x0C, 0x84,
						  0x9C, 0xF8, 0x42, 0x41, 0x48, 0x4B, 0xFE, 0x48,
						  0xF6, 0x1D, 0x59, 0xA5, 0xB1, 0x6B, 0xA0, 0x6E,
						  0x6E, 0x12, 0xD1, 0xDA, 0x27, 0xC5, 0x24, 0x9A };

	uint8_t G[SIZE/8*2] = { 0x42, 0x1D, 0xEB, 0xD6, 0x1B, 0x62, 0xEA, 0xB6,
							0x74, 0x64, 0x34, 0xEB, 0xC3, 0xCC, 0x31, 0x5E,
							0x32, 0x22, 0x0B, 0x3B, 0xAD, 0xD5, 0x0B, 0xDC,
							0x4C, 0x4E, 0x6C, 0x14, 0x7F, 0xED, 0xD4, 0x3D,
							0x06, 0x80, 0x51, 0x2B, 0xCB, 0xB4, 0x2C, 0x07,
							0xD4, 0x73, 0x49, 0xD2, 0x15, 0x3B, 0x70, 0xC4,
							0xE5, 0xD7, 0xFD, 0xFC, 0xBF, 0xA3, 0x6E, 0xA1,
							0xA8, 0x58, 0x41, 0xB9, 0xE4, 0x6E, 0x09, 0xA2 };

	uint8_t n[SIZE/8] = { 0x85, 0x42, 0xD6, 0x9E, 0x4C, 0x04, 0x4F, 0x18,
						  0xE8, 0xB9, 0x24, 0x35, 0xBF, 0x6F, 0xF7, 0xDD,
						  0x29, 0x77, 0x20, 0x63, 0x04, 0x85, 0x62, 0x8D,
						  0x5A, 0xE7, 0x4E, 0xE7, 0xC3, 0x2E, 0x79, 0xB7 };

	uint8_t dA[SIZE/8] = { 0x12, 0x8B, 0x2F, 0xA8, 0xBD, 0x43, 0x3C, 0x6C,
						   0x06, 0x8C, 0x8D, 0x80, 0x3D, 0xFF, 0x79, 0x79,
						   0x2A, 0x51, 0x9A, 0x55, 0x17, 0x1B, 0x1B, 0x65,
						   0x0C, 0x23, 0x66, 0x1D, 0x15, 0x89, 0x72, 0x63 };

	uint8_t PA[SIZE/8*2] = { 0x0A, 0xE4, 0xC7, 0x79, 0x8A, 0xA0, 0xF1, 0x19,
							 0x47, 0x1B, 0xEE, 0x11, 0x82, 0x5B, 0xE4, 0x62,
							 0x02, 0xBB, 0x79, 0xE2, 0xA5, 0x84, 0x44, 0x95,
							 0xE9, 0x7C, 0x04, 0xFF, 0x4D, 0xF2, 0x54, 0x8A,
							 0x7C, 0x02, 0x40, 0xF8, 0x8F, 0x1C, 0xD4, 0xE1,
							 0x63, 0x52, 0xA7, 0x3C, 0x17, 0xB7, 0xF1, 0x6F,
							 0x07, 0x35, 0x3E, 0x53, 0xA1, 0x76, 0xD6, 0x84,
							 0xA9, 0xFE, 0x0C, 0x6B, 0xB7, 0x98, 0xE8, 0x57 };

	uint8_t ZA[SIZE/8] = { 0xF4, 0xA3, 0x84, 0x89, 0xE3, 0x2B, 0x45, 0xB6,
						   0xF8, 0x76, 0xE3, 0xAC, 0x21, 0x68, 0xCA, 0x39,
						   0x23, 0x62, 0xDC, 0x8F, 0x23, 0x45, 0x9C, 0x1D,
						   0x11, 0x46, 0xFC, 0x3D, 0xBF, 0xB7, 0xBC, 0x9A };

	uint8_t id[] = "ALICE123@YAHOO.COM";
	uint8_t m[] = "message digest";
	uint8_t rs[SIZE/8*2] = { 0x40, 0xF1, 0xEC, 0x59, 0xF7, 0x93, 0xD9, 0xF4,
							 0x9E, 0x09, 0xDC, 0xEF, 0x49, 0x13, 0x0D, 0x41,
							 0x94, 0xF7, 0x9F, 0xB1, 0xEE, 0xD2, 0xCA, 0xA5,
							 0x5B, 0xAC, 0xDB, 0x49, 0xC4, 0xE7, 0x55, 0xD1,
							 0x6F, 0xC6, 0xDA, 0xC3, 0x2C, 0x5D, 0x5C, 0xF1,
							 0x0C, 0x77, 0xDF, 0xB2, 0x0F, 0x7C, 0x2E, 0xB6,
							 0x67, 0xA4, 0x57, 0x87, 0x2F, 0xB0, 0x9E, 0xC5,
							 0x63, 0x27, 0xA6, 0x7E, 0xC7, 0xDE, 0xEB, 0xE7 };

	uint8_t k[SIZE/8] = { 0x6C, 0xB2, 0x8D, 0x99, 0x38, 0x5C, 0x17, 0x5C,
						  0x94, 0xF9, 0x4E, 0x93, 0x48, 0x17, 0x66, 0x3F,
						  0xC1, 0x76, 0xD9, 0x25, 0xDD, 0x72, 0xB7, 0x27,
						  0x26, 0x0D, 0xBA, 0xAE, 0x1F, 0xB2, 0xF9, 0x6F };

	ECFpWork ec;
	ECFp_Initialize(&ec, 256, p, a, b);
	SM2_Initialize(&sm2, 256, G, n, (ECWork*)&ec);

/*	{	// 测试私钥生成公钥
		uint8_t PA1[SIZE/8*2];
		SM2_PublicKey(&sm2, dA, PA1);
		r = memcmp(PA, PA1, sizeof(PA)) == 0 && r;
	}

	{	// 测试计算ZA
		uint8_t ZA1[SM3_HASH_LENGTH];
		SM2_ZA(&sm2, id, strlen(id), PA, ZA1);
		r = memcmp(ZA, ZA1, sizeof(ZA)) == 0 && r;
	}

	{	// 测试签名
		uint8_t rs1[SIZE/8*2];
		SM2_Sign(&sm2, dA, ZA, m, strlen(m), k, rs1);
		r = memcmp(rs, rs1, sizeof(rs)) == 0 && r;
	}

	{	// 测试验签
		bool rz = SM2_Verify(&sm2, PA, ZA, m, strlen(m), rs);
		r = rz && r; 
	}
	*/ 
	{	// 测试签名和验签
		uint8_t m[] = "test message";
		uint8_t rs[SIZE/8*2];
		SM2_Sign(&sm2, dA, ZA, m, strlen(m), k, rs);
		bool rz = SM2_Verify(&sm2, PA, ZA, m, strlen(m), rs);
		r = rz && r;
		rs[2]++;
		rz = SM2_Verify(&sm2, PA, ZA, m, strlen(m), rs);
		r = !rz && r;


	}
	return r;
}


bool Test_SM2Fp_Encrypt_Decrypt() {

#define SIZE 256
	bool r = true;
	SM2Work sm2;
	const size_t size = SIZE;
	uint8_t p[SIZE/8] = { 0x85, 0x42, 0xD6, 0x9E, 0x4c, 0x04, 0x4f, 0x18, 
					  	  0xe8, 0xB9, 0x24, 0x35, 0xBF, 0x6f, 0xf7, 0xde,
						  0x45, 0x72, 0x83, 0x91, 0x5C, 0x45, 0x51, 0x7D,
						  0x72, 0x2E, 0xDB, 0x8B, 0x08, 0xF1, 0xDF, 0xC3 };
	
	uint8_t a[SIZE/8] = { 0x78, 0x79, 0x68, 0xB4, 0xFA, 0x32, 0xC3, 0xFD,
						  0x24, 0x17, 0x84, 0x2E, 0x73, 0xBB, 0xFE, 0xFF,
						  0x2F, 0x3C, 0x84, 0x8B, 0x68, 0x31, 0xD7, 0xE0,
						  0xEC, 0x65, 0x22, 0x8B, 0x39, 0x37, 0xE4, 0x98 };

	uint8_t b[SIZE/8] = { 0x63, 0xE4, 0xC6, 0xD3, 0xB2, 0x3B, 0x0C, 0x84,
						  0x9C, 0xF8, 0x42, 0x41, 0x48, 0x4B, 0xFE, 0x48,
						  0xF6, 0x1D, 0x59, 0xA5, 0xB1, 0x6B, 0xA0, 0x6E,
						  0x6E, 0x12, 0xD1, 0xDA, 0x27, 0xC5, 0x24, 0x9A };

	uint8_t G[SIZE/8*2] = { 0x42, 0x1D, 0xEB, 0xD6, 0x1B, 0x62, 0xEA, 0xB6,
							0x74, 0x64, 0x34, 0xEB, 0xC3, 0xCC, 0x31, 0x5E,
							0x32, 0x22, 0x0B, 0x3B, 0xAD, 0xD5, 0x0B, 0xDC,
							0x4C, 0x4E, 0x6C, 0x14, 0x7F, 0xED, 0xD4, 0x3D,
							0x06, 0x80, 0x51, 0x2B, 0xCB, 0xB4, 0x2C, 0x07,
							0xD4, 0x73, 0x49, 0xD2, 0x15, 0x3B, 0x70, 0xC4,
							0xE5, 0xD7, 0xFD, 0xFC, 0xBF, 0xA3, 0x6E, 0xA1,
							0xA8, 0x58, 0x41, 0xB9, 0xE4, 0x6E, 0x09, 0xA2 };

	uint8_t n[SIZE/8] = { 0x85, 0x42, 0xD6, 0x9E, 0x4C, 0x04, 0x4F, 0x18,
						  0xE8, 0xB9, 0x24, 0x35, 0xBF, 0x6F, 0xF7, 0xDD,
						  0x29, 0x77, 0x20, 0x63, 0x04, 0x85, 0x62, 0x8D,
						  0x5A, 0xE7, 0x4E, 0xE7, 0xC3, 0x2E, 0x79, 0xB7 };

	uint8_t dB[SIZE/8] = { 0x16, 0x49, 0xAB, 0x77, 0xA0, 0x06, 0x37, 0xBD,
						   0x5E, 0x2E, 0xFE, 0x28, 0x3F, 0xBF, 0x35, 0x35,
						   0x34, 0xAA, 0x7F, 0x7C, 0xB8, 0x94, 0x63, 0xF2,
						   0x08, 0xDD, 0xBC, 0x29, 0x20, 0xBB, 0x0D, 0xA0 };

	uint8_t PB[SIZE/8*2] = { 0x43, 0x5B, 0x39, 0xCC, 0xA8, 0xF3, 0xB5, 0x08,
							 0xC1, 0x48, 0x8A, 0xFC, 0x67, 0xBE, 0x49, 0x1A,
							 0x0F, 0x7B, 0xA0, 0x7E, 0x58, 0x1A, 0x0E, 0x48,
							 0x49, 0xA5, 0xCF, 0x70, 0x62, 0x8A, 0x7E, 0x0A,
							 0x75, 0xDD, 0xBA, 0x78, 0xF1, 0x5F, 0xEE, 0xCB,
							 0x4C, 0x78, 0x95, 0xE2, 0xC1, 0xCD, 0xF5, 0xFE,
							 0x01, 0xDE, 0xBB, 0x2C, 0xDB, 0xAD, 0xF4, 0x53,
    						 0x99, 0xCC, 0xF7, 0x7B, 0xBA, 0x07, 0x6A, 0x42 };
							 
	uint8_t k[SIZE/8] = { 0x4C, 0x62, 0xEE, 0xFD, 0x6E, 0xCF, 0xC2, 0xB9,
	         			  0x5B, 0x92, 0xFD, 0x6C, 0x3D, 0x95, 0x75, 0x14,
						  0x8A, 0xFA, 0x17, 0x42, 0x55, 0x46, 0xD4, 0x90,
						  0x18, 0xE5, 0x38, 0x8D, 0x49, 0xDD, 0x7B, 0x4F };

	uint8_t M[] = {       0x65, 0x6E, 0x63, 0x72, 0x79, 0x70, 0x74,
					0x69, 0x6F, 0x6E, 0x20, 0x73, 0x74, 0x61, 0x6E,
					0x64, 0x61, 0x72, 0x64 };
					
	uint8_t C[] = { 0x04, 0x24, 0x5C, 0x26, 0xFB, 0x68, 0xB1, 0xDD,
					0xDD, 0xB1, 0x2C, 0x4B, 0x6B, 0xF9, 0xF2, 0xB6,
					0xD5, 0xFE, 0x60, 0xA3, 0x83, 0xB0, 0xD1, 0x8D,
					0x1C, 0x41, 0x44, 0xAB, 0xF1, 0x7F, 0x62, 0x52,
					0xE7, 0x76, 0xCB, 0x92, 0x64, 0xC2, 0xA7, 0xE8,
					0x8E, 0x52, 0xB1, 0x99, 0x03, 0xFD, 0xC4, 0x73,
					0x78, 0xF6, 0x05, 0xE3, 0x68, 0x11, 0xF5, 0xC0,
					0x74, 0x23, 0xA2, 0x4B, 0x84, 0x40, 0x0F, 0x01,
					0xB8, 0x65, 0x00, 0x53, 0xA8, 0x9B, 0x41, 0xC4,
					0x18, 0xB0, 0xC3, 0xAA, 0xD0, 0x0D, 0x88, 0x6C,
					0x00, 0x28, 0x64, 0x67, 0x9C, 0x3D, 0x73, 0x60,
					0xC3, 0x01, 0x56, 0xFA, 0xB7, 0xC8, 0x0A, 0x02,
					0x76, 0x71, 0x2D, 0xA9, 0xD8, 0x09, 0x4A, 0x63,
					0x4B, 0x76, 0x6D, 0x3A, 0x28, 0x5E, 0x07, 0x48,
					0x06, 0x53, 0x42, 0x6D };

	ECFpWork ec;
	ECFp_Initialize(&ec, 256, p, a, b);
	SM2_Initialize(&sm2, 256, G, n, (ECWork*)&ec);

	{ // 测试加密数据
		size_t plainLength = sizeof(M);
		size_t cipherLength = plainLength + SM2_CipherStubLength(&sm2);
		uint8_t cipher[cipherLength];

		SM2_Encrypt(&sm2, PB, k, M, plainLength, cipher);
		r = memcmp(cipher, C, sizeof(cipherLength)) == 0 && r;
	}
	
	{ // 测试加密数据
		size_t cipherLength = sizeof(C);
		size_t plainLength = cipherLength - SM2_CipherStubLength(&sm2);
		uint8_t plain[plainLength];

		SM2_Decrypt(&sm2, dB, C, cipherLength, plain);

		r = memcmp(plain, M, plainLength) == 0 && r;
	}
	
	return r;
#undef SIZE
}

bool Test_SM2F2m_Sign_Verify() {
#define SIZE 257
	bool r = true;
	SM2Work sm2;
	const size_t size = SIZE;
	uint8_t p[(SIZE+7)/8] = { 0x02,
							  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
					  	      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01 };
	
	uint8_t a[(SIZE+7)/8] = { 0x00, 
							  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

	uint8_t b[(SIZE+7)/8] = { 0x00,
							  0xE7, 0x8B, 0xCD, 0x09, 0x74, 0x6C, 0x20, 0x23,
						  	  0x78, 0xA7, 0xE7, 0x2B, 0x12, 0xBC, 0xE0, 0x02,
						      0x66, 0xB9, 0x62, 0x7E, 0xCB, 0x0B, 0x5A, 0x25,
						      0x36, 0x7A, 0xD1, 0xAD, 0x4C, 0xC6, 0x24, 0x2B };

	uint8_t G[(SIZE+7)/8*2] = { 0x00,
								0xCD, 0xB9, 0xCA, 0x7F, 0x1E, 0x6B, 0x04, 0x41,
								0xF6, 0x58, 0x34, 0x3F, 0x4B, 0x10, 0x29, 0x7C,
								0x0E, 0xF9, 0xB6, 0x49, 0x10, 0x82, 0x40, 0x0A,
								0x62, 0xE7, 0xA7, 0x48, 0x57, 0x35, 0xFA, 0xDD,
								0x01,
								0x3D, 0xE7, 0x4D, 0xA6, 0x59, 0x51, 0xC4, 0xD7,
								0x6D, 0xC8, 0x92, 0x20, 0xD5, 0xF7, 0x77, 0x7A,
								0x61, 0x1B, 0x1C, 0x38, 0xBA, 0xE2, 0x60, 0xB1,
								0x75, 0x95, 0x1D, 0xC8, 0x06, 0x0C, 0x2B, 0x3E };

	uint8_t n[(SIZE+7)/8-1] = { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
						  	  	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
						  	  	0xBC, 0x97, 0x2C, 0xF7, 0xE6, 0xB6, 0xF9, 0x00,
						  	  	0x94, 0x5B, 0x3C, 0x6A, 0x0C, 0xF6, 0x16, 0x1D };

	uint8_t dA[SIZE/8] = { 0x77, 0x1E, 0xF3, 0xDB, 0xFF, 0x5F, 0x1C, 0xDC,
						   0x32, 0xB9, 0xC5, 0x72, 0x93, 0x04, 0x76, 0x19,
						   0x19, 0x98, 0xB2, 0xBF, 0x7C, 0xB9, 0x81, 0xD7,
						   0xF5, 0xB3, 0x92, 0x02, 0x64, 0x5F, 0x09, 0x31 };

	uint8_t PA[(SIZE+7)/8*2] = { 0x01,
								 0x65, 0x96, 0x16, 0x45, 0x28, 0x1A, 0x86, 0x26,
							 	 0x60, 0x7B, 0x91, 0x7F, 0x65, 0x7D, 0x7E, 0x93,
							     0x82, 0xF1, 0xEA, 0x5C, 0xD9, 0x31, 0xF4, 0x0F,
							     0x66, 0x27, 0xF3, 0x57, 0x54, 0x26, 0x53, 0xB2,
								 0x01, 
								 0x68, 0x65, 0x22, 0x13, 0x0D, 0x59, 0x0F, 0xB8,
							 	 0xDE, 0x63, 0x5D, 0x8F, 0xCA, 0x71, 0x5C, 0xC6,
							     0xBF, 0x3D, 0x05, 0xBE, 0xF3, 0xF7, 0x5D, 0xA5,
							     0xD5, 0x43, 0x45, 0x44, 0x48, 0x16, 0x66, 0x12 };

	uint8_t ZA[SIZE/8] = { 0x26, 0x35, 0x2A, 0xF8, 0x2E, 0xC1, 0x9F, 0x20,
						   0x7B, 0xBC, 0x6F, 0x94, 0x74, 0xE1, 0x1E, 0x90,
						   0xCE, 0x0F, 0x7D, 0xDA, 0xCE, 0x03, 0xB2, 0x7F,
						   0x80, 0x18, 0x17, 0xE8, 0x97, 0xA8, 0x1F, 0xD5 };

	uint8_t id[] = "ALICE123@YAHOO.COM";
	uint8_t m[] = "message digest";
	uint8_t rs[SIZE/8*2] = { 0x6D, 0x3F, 0xBA, 0x26, 0xEA, 0xB2, 0xA1, 0x05,
							 0x4F, 0X5D, 0x19, 0x83, 0x32, 0xE3, 0x35, 0x81, 
							 0x7C, 0x8A, 0xC4, 0x53, 0xED, 0x26, 0xD3, 0x39,
							 0x1C, 0xD4, 0x43, 0x9D, 0x82, 0x5B, 0xF2, 0x5B,
							 0x31, 0x24, 0xC5, 0x68, 0x8D, 0x95, 0xF0, 0xA1,
							 0x02, 0x52, 0xA9, 0xBE, 0xD0, 0x33, 0xBE, 0xC8,
							 0x44, 0x39, 0xDA, 0x38, 0x46, 0x21, 0xB6, 0xD6,
							 0xFA, 0xD7, 0x7F, 0x94, 0xB7, 0x4A, 0x95, 0x56 };

	uint8_t k[SIZE/8] = { 0x36, 0xCD, 0x79, 0xFC, 0x8E, 0x24, 0xB7, 0x35,
						  0x7A, 0x8A, 0x7B, 0x4A, 0x46, 0xD4, 0x54, 0xC3,
						  0x97, 0x70, 0x3D, 0x64, 0x98, 0x15, 0x8C, 0x60,
						  0x53, 0x99, 0xB3, 0x41, 0xAD, 0xA1, 0x86, 0xD6 };

	ECF2mWork ec;
	ECF2m_Initialize(&ec, SIZE, p, a, b);
	SM2_Initialize(&sm2, SIZE-1, G, n, (ECWork*)&ec);


	{	// 测试私钥生成公钥
		uint8_t PA1[SIZE/8*2];
		SM2_PublicKey(&sm2, dA, PA1);
		r = memcmp(PA, PA1, sizeof(PA)) == 0 && r;
	}

	{	// 测试计算ZA
		uint8_t ZA1[SM3_HASH_LENGTH];
		SM2_ZA(&sm2, id, strlen(id), PA, ZA1);
		r = memcmp(ZA, ZA1, sizeof(ZA)) == 0 && r;
	}

	{	// 测试签名
		uint8_t rs1[SIZE/8*2];
		SM2_Sign(&sm2, dA, ZA, m, strlen(m), k, rs1);
		r = memcmp(rs, rs1, sizeof(rs)) == 0 && r;
	}

	{	// 测试验签
		bool rz = SM2_Verify(&sm2, PA, ZA, m, strlen(m), rs);
		r = rz && r; 
	}
	{	// 测试签名和验签
		uint8_t m[] = "test message";
		uint8_t rs[SIZE/8*2];
		SM2_Sign(&sm2, dA, ZA, m, strlen(m), k, rs);
		bool rz = SM2_Verify(&sm2, PA, ZA, m, strlen(m), rs);
		r = rz && r;
		rs[2]++;
		rz = SM2_Verify(&sm2, PA, ZA, m, strlen(m), rs);
		r = !rz && r;


	}
	 
	return r;
}


bool Test_SM2F2m_Encrypt_Decrypt() {

#define LEC_BSIZE 	257
#define LEC_SIZE 	((LEC_BSIZE+7)/8)
#define LSM2_BSIZE	256
#define LSM2_SIZE 	((LSM2_BSIZE+7)/8)
	bool r = true;
	SM2Work sm2;
	const size_t size = SIZE;

	uint8_t p[LEC_SIZE] = { 0x02,
							  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
					  	      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01 };
	
	uint8_t a[LEC_SIZE] = { 0x00, 
							  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

	uint8_t b[LEC_SIZE] = { 0x00,
							  0xE7, 0x8B, 0xCD, 0x09, 0x74, 0x6C, 0x20, 0x23,
						  	  0x78, 0xA7, 0xE7, 0x2B, 0x12, 0xBC, 0xE0, 0x02,
						      0x66, 0xB9, 0x62, 0x7E, 0xCB, 0x0B, 0x5A, 0x25,
						      0x36, 0x7A, 0xD1, 0xAD, 0x4C, 0xC6, 0x24, 0x2B };

	uint8_t G[LEC_SIZE*2] = { 0x00,
								0xCD, 0xB9, 0xCA, 0x7F, 0x1E, 0x6B, 0x04, 0x41,
								0xF6, 0x58, 0x34, 0x3F, 0x4B, 0x10, 0x29, 0x7C,
								0x0E, 0xF9, 0xB6, 0x49, 0x10, 0x82, 0x40, 0x0A,
								0x62, 0xE7, 0xA7, 0x48, 0x57, 0x35, 0xFA, 0xDD,
								0x01,
								0x3D, 0xE7, 0x4D, 0xA6, 0x59, 0x51, 0xC4, 0xD7,
								0x6D, 0xC8, 0x92, 0x20, 0xD5, 0xF7, 0x77, 0x7A,
								0x61, 0x1B, 0x1C, 0x38, 0xBA, 0xE2, 0x60, 0xB1,
								0x75, 0x95, 0x1D, 0xC8, 0x06, 0x0C, 0x2B, 0x3E };

	uint8_t n[LSM2_SIZE] = { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
						  	  	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
						  	  	0xBC, 0x97, 0x2C, 0xF7, 0xE6, 0xB6, 0xF9, 0x00,
						  	  	0x94, 0x5B, 0x3C, 0x6A, 0x0C, 0xF6, 0x16, 0x1D };

	uint8_t dB[LSM2_SIZE] = { 0x56, 0xA2, 0x70, 0xD1, 0x73, 0x77, 0xAA, 0x9A,
						   0x36, 0x7C, 0xFA, 0x82, 0xE4, 0x6F, 0xA5, 0x26,
						   0x77, 0x13, 0xA9, 0xB9, 0x11, 0x01, 0xD0, 0x77,
						   0x7B, 0x07, 0xFC, 0xE0, 0x18, 0xC7, 0x57, 0xEB };

	uint8_t PB[LEC_SIZE*2] = { 0x00,
								 0xA6, 0x79, 0x41, 0xE6, 0xDE, 0x8A, 0x61, 0x80,
							 	 0x5F, 0x7B, 0xCF, 0xF0, 0x98, 0x5B, 0xB3, 0xBE,
							     0xD9, 0x86, 0xF1, 0xC2, 0x97, 0xE4, 0xD8, 0x88,
							     0x0D, 0x82, 0xB8, 0x21, 0xC6, 0x24, 0xEE, 0x57,
								 0x01, 
								 0x93, 0xED, 0x5A, 0x67, 0x07, 0xB5, 0x90, 0x87,
							 	 0x81, 0xB8, 0x60, 0x84, 0x10, 0x85, 0xF5, 0x2E,
							     0xEF, 0xA7, 0xFE, 0x32, 0x9A, 0x5C, 0x81, 0x18,
							     0x43, 0x53, 0x3A, 0x87, 0x4D, 0x02, 0x72, 0x71 };


	uint8_t k[LSM2_SIZE] = { 0x6D, 0x3B, 0x49, 0x71, 0x53, 0xE3, 0xE9, 0x25,
	         			  0x24, 0xE5, 0xC1, 0x22, 0x68, 0x2D, 0xBD, 0xC8,
						  0x70, 0x50, 0x62, 0xE2, 0x0B, 0x91, 0x7A, 0x5F,
						  0x8F, 0xCD, 0xB8, 0xEE, 0x4C, 0x66, 0x66, 0x3D };

	uint8_t M[] = {       0x65, 0x6E, 0x63, 0x72, 0x79, 0x70, 0x74,
					0x69, 0x6F, 0x6E, 0x20, 0x73, 0x74, 0x61, 0x6E,
					0x64, 0x61, 0x72, 0x64 };
					
	uint8_t C[] = { 0x04, 0x01, 0x9D, 0x23, 0x6D, 0xDB, 0x30, 0x50,
					0x09, 0xAD, 0x52, 0xC5, 0x1B, 0xB9, 0x32, 0x70,
					0x9B, 0xD5, 0x34, 0xD4, 0x76, 0xFB, 0xB7, 0xB0,
					0xDF, 0x95, 0x42, 0xA8, 0xA4, 0xD8, 0x90, 0xA3,
					0xF2, 0xE1, 0x00, 0xB2, 0x3B, 0x93, 0x8D, 0xC0,
					0xA9, 0x4D, 0x1D, 0xF8, 0xF4, 0x2C, 0xF4, 0x5D,
					0x2D, 0x66, 0x01, 0xBF, 0x63, 0x8C, 0x3D, 0x7D,
					0xE7, 0x5A, 0x29, 0xF0, 0x2A, 0xFB, 0x7E, 0x45,
					0xE9, 0x17, 0x71, 0xFD, 0x55, 0xAC, 0x62, 0x13,
					0xC2, 0xA8, 0xA0, 0x40, 0xE4, 0xCA, 0xB5, 0xB2,
					0x6A, 0x9C, 0xFC, 0xDA, 0x73, 0x73, 0x73, 0xA4,
					0x86, 0x25, 0xD3, 0x75, 0x8F, 0xA3, 0x7B, 0x3E,
					0xAB, 0x80, 0xE9, 0xCF, 0xCA, 0xBA, 0x66, 0x5E,
					0x31, 0x99, 0xEA, 0x15, 0xA1, 0xFA, 0x81, 0x89,
					0xD9, 0x6F, 0x57, 0x91, 0x25, 0xE4 };

	ECF2mWork ec;
	ECF2m_Initialize(&ec, LEC_BSIZE, p, a, b);
	SM2_Initialize(&sm2, LSM2_BSIZE, G, n, (ECWork*)&ec);

	{ // 测试加密数据
		size_t plainLength = sizeof(M);
		size_t cipherLength = plainLength + SM2_CipherStubLength(&sm2);
		uint8_t cipher[cipherLength];

		SM2_Encrypt(&sm2, PB, k, M, plainLength, cipher);
		r = memcmp(cipher, C, sizeof(cipherLength)) == 0 && r;
	}
	
	{ // 测试加密数据
		size_t cipherLength = sizeof(C);
		size_t plainLength = cipherLength - SM2_CipherStubLength(&sm2);
		uint8_t plain[plainLength];

		SM2_Decrypt(&sm2, dB, C, cipherLength, plain);

		r = memcmp(plain, M, plainLength) == 0 && r;
	}
	
	return r;
}

bool SM_Test_SM2() {
	bool r = true;
	
	//r = SM_TEST(Test_SM2Fp_Sign_Verify()) && r;
	//r = SM_TEST(Test_SM2Fp_Encrypt_Decrypt()) && r;

	//r = SM_TEST(Test_SM2F2m_Sign_Verify()) && r;
	r = SM_TEST(Test_SM2F2m_Encrypt_Decrypt()) && r;
	return r;
}
